package site.lyun.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import site.lyun.entity.Goods;
import site.lyun.entity.GoodsInf;
import site.lyun.entity.PageBean;
import site.lyun.entity.User;
import site.lyun.service.GoodsService;
import site.lyun.service.UserService;
import java.io.*;
import java.util.UUID;

public class GoodsAction extends ActionSupport {
    private Goods goods;
    private File imageFile;
    private String imageFileFileName;
    private String imageFileContentType;
    private GoodsService goodsService;
    private UserService userService;
    private int page;
    private String userName;

    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public Goods getGoods() {
        return goods;
    }
    public void setGoods(Goods goods) {
        this.goods = goods;
    }
    public GoodsService getGoodsService() {
        return goodsService;
    }
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }
    public File getImageFile() {
        return imageFile;
    }
    public void setImageFile(File imageFile) {
        this.imageFile = imageFile;
    }
    public String getImageFileFileName() {
        return imageFileFileName;
    }
    public void setImageFileFileName(String imageFileFileName) {
        this.imageFileFileName = imageFileFileName;
    }
    public String getImageFileContentType() {
        return imageFileContentType;
    }
    public void setImageFileContentType(String imageFileContentType) {
        this.imageFileContentType = imageFileContentType;
    }


    public String GoodsUpload() throws Exception {
        if(goods == null){
            return ERROR;
        }
        String address = this.SaveImage();
        address = address.replace("\\","/");
        goods.setGoodsImageAddress(address);
        boolean judge = goodsService.GoodsUpload(goods);
        if(judge == true){
            return SUCCESS;
        }
        return ERROR;
    }

    public java.lang.String GoodsFind() throws Exception {
        page = page<=0 ? 1:page;
        PageBean pageBean = goodsService.GoodsFind(page);

        if(pageBean != null){
            ServletActionContext.getRequest().setAttribute("pageBean",pageBean);
        }
        return SUCCESS;
    }

    public String GoodsShow() throws Exception {
        Goods tradeGoods = goodsService.GoodsShow(goods.getGoodsId());
        if(tradeGoods != null){
            ServletActionContext.getRequest().setAttribute("tradeGoods",tradeGoods);
            return SUCCESS;
        }
        return ERROR;
    }

    public String GoodsBuy() throws Exception {
        User user = new User();
        user.setUserName(userName);
        user = userService.find(user);
        goods = goodsService.GoodsShow(goods.getGoodsId());
        boolean judge = goodsService.GoodsBuy(goods,user);
        if(user.getUserId() != null && judge == true){
            return SUCCESS;
        }
        return ERROR;
    }

    public String GoodsTradeInf() throws Exception {
        page = page<=0 ? 1:page;
        PageBean<GoodsInf> pageBean = goodsService.GoodsTradeInf(page);
        if(pageBean != null){
            ServletActionContext.getRequest().setAttribute("pageBean",pageBean);
        }
        return SUCCESS;
    }

    public String SaveImage(){
        String fileType = imageFileFileName.substring(imageFileFileName.lastIndexOf("."));
        String realPath = ServletActionContext.getServletContext().getRealPath("/");
        String Tuuid = UUID.randomUUID().toString().replace("-","");
        String filePath = realPath+"photo\\" + Tuuid+fileType;
        String webPath = "photo\\" + Tuuid+fileType;
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(imageFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            file = new File(filePath);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return webPath;
    }
}
